$(function() {

	
  });
$("#add-a-package").click(function(){
	$('#package-temp .package').clone().prependTo('#package-list');
	$("#package-list .package:hidden").first().show({effect:"blind",direction:"up"},1,function(){
		resetPackageList();
		addRemovePackageEvent();
	});
});
function addRemovePackageEvent(){
	$("#package-list .package").each(function(){
		var that=this;
		$(that).find(".trash").unbind("click");
		$(that).find(".trash").click(function(){
			var self=this;
			var removeId=$(self).attr("ref-id");
			$(removeId).hide({effect:"blind",direction:"up"},1000,function(){
				$(removeId).remove();
				resetPackageList();
			});
		})
	})
}
addRemovePackageEvent();
function resetPackageList(){
	$("#package-list .package").each(function(idx,ele){
		$(ele).attr("id","package-"+idx);
		$(ele).find(".trash").attr("ref-id","#package-"+idx);
		$(ele).find(".name").html("Package "+(idx+1));
		$(ele).find(".errContainer label").each(function(){
			var indexOfLastDash=$(this).attr("for").lastIndexOf("-")
			var orignalValue=$(this).attr("for").substring(0,indexOfLastDash);
			$(this).attr("for",orignalValue+"-"+idx);
		})
		$(ele).find("input[id!='']").each(function(){
			var indexOfLastDash=$(this).attr("id").lastIndexOf("-")
			var orignalValue=$(this).attr("id").substring(0,indexOfLastDash);
			$(this).attr("id",orignalValue+"-"+idx);
		})
	});
	if ($("#package-list .package").length>1){
		$("#package-list .package .trash").show();
	} else{
		$("#package-list .package .trash").hide();
	}
	$(".dms .total-packages .count").text($("#package-list .package").length);
}


	$("form input[name='schedulePickupDateList.pickupDateScheduled']").datetimepicker({minDate:0});
	$("form input[name='shippingDate']").datepicker({minDate:+1});
  
  //add validator rule for date of birth, not allow future date
	$.validator.addMethod("dateNotInPast", function(value, element) {
        var now = new Date();
        var valid=now.getTime() <= Date.parse(value) || value == "";
        return valid;
    }, "Date cannot be in the past.");
	  var minYear=new Date().getYear();
	$(".dms form[name='dms-place-order-form']").validate({
		errorContainer:	(".dms form[name='dms-place-order-form'] div.errContainer" ),
		rules:{
			
		},
		messages:{
			"sender.firstName":"Missing first name.",
			"sender.lastName":"Missing last name.",
			"sender.source.addressLine1":"Missing address.",
			"sender.source.city":"Missing city.",
			"sender.source.state":"Missing state.",
			"sender.source.zip":{
				required:"Missing zip code.",
				minlength:"minimum 5 digits.",
				maxlength:"maximum 5 digits."
			},
			"sender.phoneNumber":"Missing phone number.",
			"recipient.firstName":"Missing first name.",
			"recipient.lastName":"Missing last name.",
			"recipient.destination.addressLine1":"Missing address.",
			"recipient.destination.city":"Missing city.",
			"recipient.destination.state":"Missing state.",
			"recipient.destination.zip":{
				required:"Missing zip code.",
				minlength:"minimum 5 digits.",
				maxlength:"maximum 5 digits."
			},
			"recipient.phoneNumber":"Missing phone number.",
			"packages.weight":{
				required:"Weight is required.",
				number:"Weight should be a nubmer."
			},
			"packages.length":{
				required:"Length is required.",
				number:" Length should be a number."
			},
			"packages.width":{
				required:"Width is required.",
				number:" Width should be a number."
			},
			"packages.height":{
				required:"Height is required.",
				number:" Height should be a number."
			},
			"packages.value":{
				number:" Value should be a number."
			},
			"schedulePickupDateList.pickupDateScheduled":{
				required:"Missing pick-up date.",
				dateNotInPast:"Date cannot be in the past."
			},
			shippingDate:{
				required:"Missing shipping date.",
				dateNotInPast:"Date cannot be in the past."
			},
			"order.serviceType":"Missing service type.",
			"payments.creditCard.creditCardType":"Missing credit card type.",
			"payments.creditCard.accountNumber":{
				required:"Missing credit card number.",
				creditcard:"credit card number is invalid."
			},
			"payments.creditCard.securityCode":"Missing security code.",
			expirationMonth:"Missing month of expiration date.",
			expirationYear:"Missing year of expiration date.",
			"payments.creditCard.nameAppearedOnCard":"Missing name on card.",
			"payments.creditCard.billingAddress.addressLine1":"Missing address.",
			"payments.creditCard.billingAddress.city":"Missing city.",
			"payments.creditCard.billingAddress.state":"Missing state.",
			"payments.creditCard.billingAddress.zip":{
				required:"Missing zip code.",
				minlength:"minimum 5 digits.",
				maxlength:"maximum 5 digits."
			}
		}	
	});
